﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class PhanQuyen : Form
    {
        public PhanQuyen()
        {
            InitializeComponent();
        }

        private void ThoatButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void xoaThongTin()
        {
            txtChucVu.Text = null;
            txtMaChucVu.Text = null;
            txtPhongBan.Text = null;
            checkboxDonHang.Checked = false;
            checkboxGopY.Checked = false;
            checkboxHoaDon.Checked = false;
            checkboxKhachHang.Checked = false;
            checkboxLienHe.Checked = false;
            checkboxNhaCungUng.Checked = false;
            checkboxNhanVien.Checked = false;
            checkboxSanPham.Checked = false;
            checkboxThongKe.Checked = false;
            checkboxTinTuc.Checked = false;
        }

        private void loadDataGridView()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select MaSo_CV,Ten_CV,Ten_PB from ChucVu,PhongBan where ChucVu.MaSo_PB = PhongBan.MaSo_PB order by Ten_PB";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "ChucVu");

            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["ChucVu"].DefaultView;
        }

        private void PhanQuyen_Load(object sender, EventArgs e)
        {
            loadDataGridView();
        }

        private void docThongTin(string MaCV)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select NhanVien,SanPham,KhachHang,HoaDon,NhaCungUng,DonHang,TinTuc,LienHe,GopY,ThongKe,MaSo_CV,Ten_CV,Ten_PB from ChucVu,PhongBan where ChucVu.MaSo_PB = PhongBan.MaSo_PB and MaSo_CV = @MaSo_CV";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_CV", MaCV);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                txtMaChucVu.Text = reader["MaSo_CV"].ToString();
                txtChucVu.Text = reader["Ten_CV"].ToString();
                txtPhongBan.Text = reader["Ten_PB"].ToString();

                if ((bool)reader["NhanVien"] == true)
                    checkboxNhanVien.Checked = true;
                else
                    checkboxNhanVien.Checked = false;

                if ((bool)reader["SanPham"] == true)
                    checkboxSanPham.Checked = true;
                else
                    checkboxSanPham.Checked = false;

                if ((bool)reader["KhachHang"] == true)
                    checkboxKhachHang.Checked = true;
                else
                    checkboxKhachHang.Checked = false;

                if ((bool)reader["HoaDon"] == true)
                    checkboxHoaDon.Checked = true;
                else
                    checkboxHoaDon.Checked = false;

                if ((bool)reader["NhaCungUng"] == true)
                    checkboxNhaCungUng.Checked = true;
                else
                    checkboxNhaCungUng.Checked = false;

                if ((bool)reader["DonHang"] == true)
                    checkboxDonHang.Checked = true;
                else
                    checkboxDonHang.Checked = false;

                if ((bool)reader["TinTuc"] == true)
                    checkboxTinTuc.Checked = true;
                else
                    checkboxTinTuc.Checked = false;

                if ((bool)reader["LienHe"] == true)
                    checkboxLienHe.Checked = true;
                else
                    checkboxLienHe.Checked = false;

                if ((bool)reader["GopY"] == true)
                    checkboxGopY.Checked = true;
                else
                    checkboxGopY.Checked = false;

                if ((bool)reader["ThongKe"] == true)
                    checkboxThongKe.Checked = true;
                else
                    checkboxThongKe.Checked = false;
            }
            reader.Close();
            conn.Close();
        }

        private void chonHetButton_Click(object sender, EventArgs e)
        {
            checkboxDonHang.Checked = true;
            checkboxGopY.Checked = true;
            checkboxHoaDon.Checked = true;
            checkboxKhachHang.Checked = true;
            checkboxLienHe.Checked = true;
            checkboxNhaCungUng.Checked = true;
            checkboxNhanVien.Checked = true;
            checkboxSanPham.Checked = true;
            checkboxThongKe.Checked = true;
            checkboxTinTuc.Checked = true;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string MSCV;
            MSCV = dataGridView1.Rows[e.RowIndex].Cells["MaSo_CV1"].Value.ToString();
            docThongTin(MSCV);
            lblThongBao.Text = null;
        }

        private void boChonButton_Click(object sender, EventArgs e)
        {
            checkboxDonHang.Checked = false;
            checkboxGopY.Checked = false;
            checkboxHoaDon.Checked = false;
            checkboxKhachHang.Checked = false;
            checkboxLienHe.Checked = false;
            checkboxNhaCungUng.Checked = false;
            checkboxNhanVien.Checked = false;
            checkboxSanPham.Checked = false;
            checkboxThongKe.Checked = false;
            checkboxTinTuc.Checked = false;
        }

        private void CapNhatButton_Click(object sender, EventArgs e)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "update ChucVu set NhanVien = @NhanVien, SanPham = @SanPham, KhachHang = @KhachHang, HoaDon = @HoaDon, NhaCungUng = @NhaCungUng, DonHang = @DonHang, TinTuc = @TinTuc, LienHe = @LienHe, GopY = @GopY, ThongKe = @ThongKe where MaSo_CV = @MaSo_CV";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();

            if (checkboxNhanVien.Checked == true)
                cmd.Parameters.AddWithValue("@NhanVien", "True");
            else
                cmd.Parameters.AddWithValue("@NhanVien", "False");

            if (checkboxSanPham.Checked == true)
                cmd.Parameters.AddWithValue("@SanPham", "True");
            else
                cmd.Parameters.AddWithValue("@SanPham", "False");

            if (checkboxKhachHang.Checked == true)
                cmd.Parameters.AddWithValue("@KhachHang", "True");
            else
                cmd.Parameters.AddWithValue("@KhachHang", "False");

            if (checkboxHoaDon.Checked == true)
                cmd.Parameters.AddWithValue("@HoaDon", "True");
            else
                cmd.Parameters.AddWithValue("@HoaDon", "False");

            if (checkboxNhaCungUng.Checked == true)
                cmd.Parameters.AddWithValue("@NhaCungUng", "True");
            else
                cmd.Parameters.AddWithValue("@NhaCungUng", "False");

            if (checkboxDonHang.Checked == true)
                cmd.Parameters.AddWithValue("@DonHang", "True");
            else
                cmd.Parameters.AddWithValue("@DonHang", "False");

            if (checkboxTinTuc.Checked == true)
                cmd.Parameters.AddWithValue("@TinTuc", "True");
            else
                cmd.Parameters.AddWithValue("@TinTuc", "False");

            if (checkboxLienHe.Checked == true)
                cmd.Parameters.AddWithValue("@LienHe", "True");
            else
                cmd.Parameters.AddWithValue("@LienHe", "False");

            if (checkboxGopY.Checked == true)
                cmd.Parameters.AddWithValue("@GopY", "True");
            else
                cmd.Parameters.AddWithValue("@GopY", "False");

            if (checkboxThongKe.Checked == true)
                cmd.Parameters.AddWithValue("@ThongKe", "True");
            else
                cmd.Parameters.AddWithValue("@ThongKe", "False");

            cmd.Parameters.AddWithValue("@MaSo_CV", txtMaChucVu.Text);
            cmd.ExecuteNonQuery();
            conn.Close();
            xoaThongTin();
            lblThongBao.Text = "Cập nhật thành công";
        }
    }
}
